﻿    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using com.iss.inventory.Entity;
    using System.Data.Objects;

    namespace com.iss.inventory.Controllers
    {
    public class LoginController
    {
        private InvManagementEntities invManEn;
        private Employee emp;
        private Department dep;

            public LoginController()
        {
            invManEn = new InvManagementEntities();
        }

        public List<string> Login(String empID,String password)
            {
                List<string> res = new List<string>();
            int empID1 =0;
            if (int.TryParse(empID, out empID1))
            {
                emp = (from e in invManEn.Employees where e.EmployeeID == empID1 && e.Password == password select e).FirstOrDefault<Employee>();
                if (emp != null)
                {

                    string EmployeeName = emp.Name.ToString();
                    res.Add(EmployeeName); //index0
                    string role = getRole(emp.EmployeeID);
                    res.Add(role);//index1
                    string department = getDepName((int)emp.DeptID);
                    res.Add(department);//index2
                    string headid = getHeadID((int)emp.DeptID).ToString();
                    res.Add(headid);//index3
                    if (CheckDelegate(emp.EmployeeID) != 0)
                   {
                        string delrole = getRole(CheckDelegate(emp.EmployeeID));
                        int temp_employeeID = CheckDelegate(emp.EmployeeID);
                        string delname = (from e in invManEn.Employees where e.EmployeeID == temp_employeeID select e.Name).First();
                        res.Add(delrole);//index4
                        res.Add(delname);//index5
                        res.Add(temp_employeeID.ToString());//index6save deligator ID
                    }

                    return res;
                }
                else
                {
                    string errormsg = "UserName and password are incorrected";
                    res.Add(errormsg);
                    return res;
                }
            }

            else
            {
                string errormsg = "UserName and password are incorrected";
                res.Add(errormsg);
                return res;
            }
        }

        public int CheckDelegate(int employeeID)
        {
            DateTime dt=DateTime.Today;
            var checkdele = (from e in invManEn.DelegateDetails
                             where EntityFunctions.TruncateTime(e.DateFrom) <= EntityFunctions.TruncateTime(DateTime.Now)
                                 && EntityFunctions.TruncateTime(e.DateTill) >= EntityFunctions.TruncateTime(DateTime.Now) && e.DelegateID == employeeID
                             select e).FirstOrDefault();
            if (checkdele != null)
            {
                return Convert.ToInt32(checkdele.DelegatorID);
            }
            else
    return 0;
                           

        }

        public String getRole(int EmployeeID )
        {
            return (from c in invManEn.Employees where c.EmployeeID == EmployeeID select c.Role).First();
            
        }
        public String getDepName(int departmentID)
        {
          
            dep = (from d in invManEn.Departments where d.DeptID==departmentID select d).FirstOrDefault();
             return dep.DeptName;
         }
        public int getHeadID(int departmentID)
        {
            return (int)(from d in invManEn.Departments where d.DeptID==departmentID select d.HeadID).First();
        }
       
    }
    }
